package com.iflyun.rabbitmqcustmer.litener;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.iflyun.rabbitmqcustmer.domain.WebLog;
import com.iflyun.rabbitmqcustmer.service.WebProducerService;

@Controller
public class WebLogLitener implements  MessageListener {
	
	 private static final Logger logger = Logger.getLogger(WebLogLitener.class);

	
	@Resource(name = "webProducerService")
	private WebProducerService webProducerService;
	
	private List<WebLog> list=new ArrayList<WebLog>();
	
	long t=System.currentTimeMillis();
	long t1=0;
	@Override
	public void onMessage(Message message) {
		String json="";
		try {
			json=new String(message.getBody(), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("json转换失败：",e);
		}
		WebLog weblog = JSON.parseObject(json, WebLog.class);
		try {
			/*webProducerService.setWebLog(weblog);
			t1=t1+(System.currentTimeMillis()-t);
			t=System.currentTimeMillis();
			System.out.println("总时间:"+t1);*/
			list.add(weblog);
			if(list.size()==1000){
				//jdbctemplate
				webProducerService.setWebLogList(list);
				//hibernate
//				webProducerService.saveWebLog(list);
				list.clear();
			}
			
		} catch (Exception e1) {
			logger.error("处理调用接口记录信息发生错误：",e1);
		}
											
	
    }

}